package channel

import (
	"context"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/user/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type LocalEWalletModel struct {
	ctx context.Context
	db  *gorm.DB
	logx.Logger
}

func (m *LocalEWalletModel) getDB() *gorm.DB {
	return m.db
}

func NewLocalEWalletModel(ctx context.Context, db *gorm.DB) *LocalEWalletModel {
	return &LocalEWalletModel{
		ctx:    ctx,
		db:     db,
		Logger: logx.WithContext(ctx),
	}
}

func (m *LocalEWalletModel) UserLocalEWalletsDel(in *rpc.UserLocalEWalletsDelRequest) (*rpc.UserLocalEWalletsDelResponse, error) {
	del := m.db.Where("id = ?", in.Id)

	tx := del.Delete(&model.UserChannelLocalEwallet{})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrDel
	}

	return &rpc.UserLocalEWalletsDelResponse{
		IsDel: true,
	}, nil
}

// ------------------user channel local ewallet ------------------
func (m *LocalEWalletModel) UserLocalEWalletsInfo(in *rpc.UserLocalEwalletInfoRequest) (*rpc.UserLocalEwalletInfoResponse, error) {
	var userLocalEwallets []*model.UserChannelLocalEwallet
	var userLocalEwalletInfoList []*rpc.UserLocalEwalletInfo

	// 查询所有用户银行信息
	query := m.db
	if in.UserId != 0 {
		query = query.Where("user_id = ?", in.UserId)
	}
	if in.Owner != "" {
		query = query.Where("owner LIKE ?", "%"+in.Owner+"%")
	}
	if in.Type != "" {
		query = query.Where("type = ?", in.Type)
	}
	if in.Phone != "" {
		query = query.Where("phone = ?", in.Phone)
	}

	query.Find(&userLocalEwallets)
	for _, userLocalEwallet := range userLocalEwallets {
		userLocalEwalletInfo := &rpc.UserLocalEwalletInfo{
			Id:          int64(userLocalEwallet.ID),
			UserId:      userLocalEwallet.UserId,
			Type:        userLocalEwallet.WalletType,
			Owner:       userLocalEwallet.Owner,
			VisionLevel: userLocalEwallet.VisionLevel,
			Phone:       userLocalEwallet.Phone,
			Code:        userLocalEwallet.Code,
			RevenueMin:  userLocalEwallet.RevenueMin,
		}
		userLocalEwalletInfoList = append(userLocalEwalletInfoList, userLocalEwalletInfo)
	}

	return &rpc.UserLocalEwalletInfoResponse{
		UserLocalEwalletInfoList: userLocalEwalletInfoList,
	}, nil
}

func (m *LocalEWalletModel) UserLocalEWalletsInsert(in *rpc.UserLocalEwalletInsertRequest) (*rpc.UserLocalEwalletInsertResponse, error) {
	tx := m.db.Create(&model.UserChannelLocalEwallet{
		UserId:      in.UserId,
		WalletType:  in.Type,
		Owner:       in.Owner,
		VisionLevel: in.VisionLevel,
		Phone:       in.Phone,
		Code:        in.Code,
		RevenueMin:  in.RevenueMin,
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrInsert
	}

	return &rpc.UserLocalEwalletInsertResponse{
		IsInsert: true,
	}, nil
}

func (m *LocalEWalletModel) UserLocalEWalletsUpdate(in *rpc.UserLocalEwalletUpdateRequest) (*rpc.UserLocalEwalletUpdateResponse, error) {
	tx := m.db.Where("id = ?", in.Id).Updates(&model.UserChannelLocalEwallet{
		UserId:      in.UserId,
		WalletType:  in.Type,
		Owner:       in.Owner,
		VisionLevel: in.VisionLevel,
		Phone:       in.Phone,
		Code:        in.Code,
		RevenueMin:  in.RevenueMin,
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrUpdate
	}

	return &rpc.UserLocalEwalletUpdateResponse{
		IsUpdate: true,
	}, nil
}

func (m *LocalEWalletModel) UserLocalEWalletsPage(in *rpc.UserLocalEWalletsPageRequest) (*rpc.UserLocalEWalletsPageResponse, error) {
	var UserChannelLocalEwalletInfos []*model.UserChannelLocalEwallet
	offset := in.Page.PageSize * (in.Page.PageNo - 1)

	// 查询所有用户银行信息
	query := m.db.Model(&model.UserChannelLocalEwallet{})
	if in.Param.UserId != 0 {
		query = query.Where("user_id = ?", in.Param.UserId)
	}
	if in.Param.Owner != "" {
		query = query.Where("owner LIKE ?", "%"+in.Param.Owner+"%")
	}
	if in.Param.Phone != "" {
		query = query.Where("phone = ?", in.Param.Phone)
	}
	query = query.Limit(int(in.Page.PageSize)).Offset(int(offset))
	tx := query.Find(&UserChannelLocalEwalletInfos)
	var userLocalEwalletInfos []*rpc.UserLocalEwalletInfo
	var total int64
	_ = query.Count(&total).Error
	if tx.Error != nil {
		return &rpc.UserLocalEWalletsPageResponse{
			DataList: userLocalEwalletInfos,
			Total:    0,
		}, xerrors.ErrDataConvert
	}
	_ = copier.Copy(&userLocalEwalletInfos, UserChannelLocalEwalletInfos)
	for i := 0; i < len(userLocalEwalletInfos); i++ {
		userLocalEwalletInfos[i].Id = UserChannelLocalEwalletInfos[i].Model.ID
		userLocalEwalletInfos[i].Type = UserChannelLocalEwalletInfos[i].WalletType
	}
	return &rpc.UserLocalEWalletsPageResponse{
		DataList: userLocalEwalletInfos,
		Total:    total,
	}, nil
}
